package com.weizhi.gateway.filter;

import java.io.IOException;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;


public class CrosFilter implements Filter{

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse)response;

        //允许某个域名调用
        res.addHeader("Access-Control-Allow-Origin", "*");
        //允许哪些方法调用
        res.addHeader("Access-Control-Allow-Methods", "*");
        //json格式的请求需要这个验证
        res.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,token");
        //告诉浏览器在1小时之内不需要发送OPTIONS预检，提高效率
        res.addHeader("Access-Control-Max-Agent", "3600");
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }

}